class AdService{
    constructor(pool){
        this.pool = pool;
    }
    getProvinces(response){
        let sql = "select * from ad_name where length(code)=2";
        this.pool.getConnection((error,connection)=>{
            if(error){
                throw error;
            }
            let json = null;
            connection.query(sql,(error,results)=>{
                if(error){
                    console.log(error);
                }
                json = {
                    result:'ok',
                    provinces:results
                }
                response.end(JSON.stringify(json));
                connection.release();
            });
        });
    }
    getCities(response,provinceCode){
        let sql = "select * from ad_name where length(code)=4 and left(code,2)=?";
        this.pool.getConnection((error,connection)=>{
            if(error){
                throw error;
            }
            let json = null;
            console.log(provinceCode);
            connection.query(sql,provinceCode,(error,results)=>{
                if(error){
                    console.log(error);
                }else{
                    if(results){
                        json = {
                            result:'ok',
                            cities:results
                        }
                    }else{
                        json = {
                            result:'nodata',
                            message:'没有数据'
                        }
                    }
                }
                response.end(JSON.stringify(json));
                connection.release();
            });
        });
    }
}

module.exports = AdService;